package com.huaguoguo.cyg.aspect;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.chrono.ChronoLocalDate;
import java.time.temporal.ChronoUnit;

/**
 * 类说明： 计算时间的切面<br/>
 * author:huaguoguo
 * Date: 2020/2/6
 * Time: 16:39
 */
@Aspect
@Slf4j
@Component
public class TimeAspect {

    /**
     * 定义切入点<br>
     *通过@Pointcut注解声明频繁使用的切点表达式<br>
     */
    @Pointcut("@annotation(com.huaguoguo.cyg.annotation.ExecTime)")
    public void timeAspect(){}

    @Around("timeAspect()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        Instant start = Instant.now();
        Object proceed = point.proceed();
        long used = ChronoUnit.MILLIS.between(start, Instant.now());
        log.info("{}耗时：{}ms",point.getSignature().getName(),used);
        return proceed;
    }

}
